package org.example.demo1;

/**
 * @author Gong.Yang
 * 运行输出：
 * 线程1状态：RUNNABLE
 * 线程1开始执行，进入等待状态
 * 线程1状态：WAITING
 * 线程2开始执行，唤醒其他线程
 * 线程1后续逻辑
 */
public class DemoRunnableToWait {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        //WAIT线程
        Thread thread1 = new Thread(() -> {
            System.out.println("线程1状态：" + Thread.currentThread().getState());
            synchronized (lock) {
                try {
                    System.out.println("线程1开始执行，进入等待状态");
                    lock.wait(); // 线程1释放锁，并进入等待状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1后续逻辑");

            }
        });

        //唤醒线程
        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("线程2开始执行，唤醒其他线程");
                lock.notify(); // 唤醒处于等待状态的线程1
            }
        });

        thread1.start();

        Thread.sleep(500); // 等待一段时间，确保线程1进入WAIT

        System.out.println("线程1状态：" + thread1.getState());

        thread2.start();
        thread1.join();
        thread2.join();

    }
}
